package com.itboye.ihomebank.activity.login;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.GestureDetector;
import android.view.GestureDetector.OnGestureListener;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.ViewFlipper;

import com.itboye.ihomebank.MainActivity;
import com.itboye.ihomebank.R;
import com.itboye.ihomebank.base.BaseOtherActivity;

import java.util.Observable;
import java.util.Observer;

public class WelcomeActivity extends BaseOtherActivity implements Observer, OnGestureListener, OnTouchListener {

	private GestureDetector detector;
	private ViewFlipper myViewFlipper, viewflipperbtn;
	private int[] imgs = { R.drawable.welcome_1, R.drawable.welcome_2, R.drawable.welcome_3 };

	TextView jump;
	SharedPreferences mySharedPreferences;

	@Override
	public void update(Observable observable, Object data) {
		// TODO Auto-generated method stub

	}


	/**
	 * 向FlipperView中动态添加View
	 */
	private void addFlipperView() {
		for (int i = 0; i < imgs.length; i++) {
			View view = LayoutInflater.from(this).inflate(R.layout.myviewflipper, null);

			ImageView iv = (ImageView) view.findViewById(R.id.view_image);

			iv.setBackgroundResource(imgs[i]);
			myViewFlipper.addView(view);

		}

	}

	@Override
	public boolean onDown(MotionEvent e) {
		return false;
	}

	@Override
	public void onShowPress(MotionEvent e) {

	}

	@Override
	public boolean onSingleTapUp(MotionEvent e) {
		return false;
	}

	@Override
	public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
		return false;
	}

	@Override
	public void onLongPress(MotionEvent e) {

	}
	@SuppressWarnings("ResourceType")
	private void showPreviousView() {

		viewflipperbtn.showPrevious();
		myViewFlipper.setInAnimation(AnimationUtils.loadAnimation(this, R.drawable.slide_right_in));
		myViewFlipper.setOutAnimation(AnimationUtils.loadAnimation(this, R.drawable.slide_right_out));
		myViewFlipper.showPrevious();
	}
	@SuppressWarnings("ResourceType")
	private void showNextView() {
		viewflipperbtn.showNext();
		myViewFlipper.setInAnimation(AnimationUtils.loadAnimation(this, R.drawable.slide_left_in));
		myViewFlipper.setOutAnimation(AnimationUtils.loadAnimation(this, R.drawable.slide_left_out));
		myViewFlipper.showNext();

	}

	@Override
	public boolean onTouch(View v, MotionEvent event) {
		return detector.onTouchEvent(event);
	}

	@Override
	public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
		// TODO Auto-generated method stub
		if (myViewFlipper.getDisplayedChild() == 0) {
			if (e1.getX() - e2.getX() >= 100) {
				showNextView();
			}
		} else if (myViewFlipper.getDisplayedChild() == 2) {
			Intent intent = new Intent(WelcomeActivity.this, MainActivity.class);
			startActivity(intent);
			finish();
			SharedPreferences.Editor editor = mySharedPreferences.edit();
			// 用putString的方法保存数据
			editor.putString("firstLogin", "true");
			// 提交当前数据
			editor.commit();
		} else {
			if (e1.getX() - e2.getX() >= 100) {
				showNextView();
			} else if (e2.getX() - e1.getX() >= 100) {
				showPreviousView();
			}
		}

		return false;
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		
	}

	@Override
	protected int layout_id() {
		// TODO Auto-generated method stub
		return R.layout.activity_welcome;
	}

	@Override
	protected void onInitController() {
		// TODO Auto-generated method stub
		
	}

	@Override
	protected void onInit() {
		// TODO Auto-generated method stub
		// 实例化SharedPreferences对象（第一步）
		mySharedPreferences = getSharedPreferences("test", Activity.MODE_PRIVATE);
		// 实例化SharedPreferences.Editor对象（第二步）

		// 使用getString方法获得value，注意第2个参数是value的默认值
		String firstLogin = mySharedPreferences.getString("firstLogin", "");

		if ("".equals(firstLogin)) {
			myViewFlipper = (ViewFlipper) findViewById(R.id.viewflipper);
			myViewFlipper.setOnTouchListener(this);
			jump = (TextView) findViewById(R.id.jump);

			viewflipperbtn = (ViewFlipper) findViewById(R.id.viewflipperbtn);
			viewflipperbtn.setOnTouchListener(this);
			myViewFlipper.setLongClickable(true);// 设置长按事件

			// myViewFlipper.setAutoStart(true);// 设置是否自动播放，默认不自动播放
			// myViewFlipper.setInAnimation(AnimationUtils.loadAnimation(this,
			// R.drawable.slide_left_in) );
			// myViewFlipper.setOutAnimation(AnimationUtils.loadAnimation(this,R.drawable.slide_left_out)
			// );
			detector = new GestureDetector(this);
			addFlipperView();

			jump.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					Intent intent = new Intent(WelcomeActivity.this, MainActivity.class);
					startActivity(intent);
					finish();
					SharedPreferences.Editor editor = mySharedPreferences.edit();
					// 用putString的方法保存数据
					editor.putString("firstLogin", "true");
					// 提交当前数据
					editor.commit();

				}
			});
		} else {
			Intent intent = new Intent(WelcomeActivity.this, SplashActivity.class);
			startActivity(intent);
			finish();
			
		}
	}

	@Override
	protected void onReadIntent(Bundle bundle) {
		// TODO Auto-generated method stub
		
	}

}
